﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.URLSource
{
    [Serializable]
    public class SQLURLSource : URLSourceBase
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionStr { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 排序方式
        /// </summary>
        public string Order { get; set; }

        public SQLURLSource()
        {
            Order = "DESC";
        }
        public override List<URLItem> GetURLCollection()
        {
            DataSet dtSet = _GetDataSet();
            var result = (from s in dtSet.Tables[0].AsEnumerable()
                          select new URLItem()
                          {
                              ID= Convert.ToInt32(s["MID"]),
                              URL = s["Url"].ToString(),
                              Status = (URLItemStatus)Enum.Parse(typeof(URLItemStatus), s["Status"].ToString())
                          }).ToList();

            return result;
            
        }

        public override void SetURLStatus(URLItem Item, URLItemStatus Status)
        {
            string SQL = string.Format("Update {0} set Status={1} where MID={2}",TableName,Convert.ToInt32( Status),Item.ID);
            _ExecuteNonQuery(SQL);
        }


        private DataSet _GetDataSet()
        {
            string SQL = string.Format(" Select  MID, Url,Status from {0} where Status<>{1} Order by MID {2}", TableName, Convert.ToInt32(URLItemStatus.Finished),Order);
            DataSet dtset= new DataSet();
            try
            {
                using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(ConnectionStr))
                {
                    sqlConnection.Open();
                    System.Data.SqlClient.SqlCommand cmd = new SqlCommand(SQL, sqlConnection);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dtset);
                    sqlConnection.Close();
                }
            }
            catch { }

            return dtset;
        }

        private bool _ExecuteNonQuery(string SQL)
        {
            int affrow = 0;
            using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(ConnectionStr))
            {
                sqlConnection.Open();
                System.Data.SqlClient.SqlCommand cmd = new SqlCommand(SQL, sqlConnection);
                affrow = cmd.ExecuteNonQuery();
                cmd.Dispose();
                sqlConnection.Close();
            }

            return affrow > 0;
        }

        [NonSerialized]
        private SQLURLSourceForm _editform;
        public override InnerFormBase GetEditForm()
        {
            if (_editform == null)
            {
                _editform = new SQLURLSourceForm();
                _editform.InfoSaveEventHandler += (sender, args) =>
                {
                    this.ConnectionStr = _editform.ConnectionStr;
                    this.TableName = _editform.TableName;
                    this.Order = _editform.Order;
                };
            }

            _editform.URLSource = this;
            return _editform;
        }

    }
}
